package com.shijiwei.trip.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.shijiwei.trip.R;
import com.shijiwei.trip.bean.cityBean.City;
import com.shijiwei.trip.task.ImageUtils;

import org.w3c.dom.Text;
import org.xutils.image.ImageOptions;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.List;

/**
 * Created by sjw on 2015/12/10.
 */
public class CityAdapter extends AppBaseAdapter {

    private List<City.ProductListEntity> list;
    List<String> tabList;
    private ImageOptions optionsHead ;
    private ImageOptions optionsPic ;

    public CityAdapter(Context context, List<City.ProductListEntity> list) {
        super(context, list);
        this.list = list;
        optionsPic = new ImageOptions.Builder()
                .setRadius(20)
                .setFailureDrawableId(R.mipmap.ic_launcher)
                .setLoadingDrawableId(R.mipmap.ic_launcher)
                .build();
        optionsHead = new ImageOptions.Builder()
                .setCircular(true)
                .setFailureDrawableId(R.mipmap.ic_launcher)
                .setLoadingDrawableId(R.mipmap.ic_launcher)
                .build();
    }

    @Override
    public View getItemView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null){
            convertView = inflater.inflate(R.layout.item_listview_city,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        x.image().bind(holder.pic, list.get(position).getTitle_page(), optionsPic);
        x.image().bind(holder.head, list.get(position).getUser().getAvatar_l(), optionsHead);
        holder.title.setText(list.get(position).getTitle());
        holder.date.setText(list.get(position).getDate_str());
        holder.location.setText(list.get(position).getAddress());
        holder.likeCount.setText(list.get(position).getLike_count() + "人喜欢");

        if (tabList != null){
            tabList.clear();
        }
        if (holder.tabList.getChildCount() > 0){
//            holder.tabList.removeAllViews();
            holder.tabList.removeAllViewsInLayout();
        }
        tabList = list.get(position).getTab_list();
        for (int i = 0; i < tabList.size(); i++) {
            TextView tab = new TextView(context);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.setMarginEnd(6);
            tab.setLayoutParams(params);
            tab.setPadding(6, 3, 6, 3);
            tab.setText(tabList.get(i));
            tab.setBackgroundResource(R.drawable.bg_text_tab_city);
            holder.tabList.addView(tab);

        }
        holder.price.setText(list.get(position).getPrice());

        return convertView;
    }

    class ViewHolder{
        public ViewHolder(View convertView){
            x.view().inject(this,convertView);
        }
        @ViewInject(R.id.iv_pic)
        ImageView pic;
        @ViewInject(R.id.iv_head)
        ImageView head;
        @ViewInject(R.id.tv_title)
        TextView title;
        @ViewInject(R.id.tv_date)
        TextView date;
        @ViewInject(R.id.tv_location)
        TextView location;
        @ViewInject(R.id.tv_likecount)
        TextView likeCount;
        @ViewInject(R.id.tv_price)
        TextView price;
        @ViewInject(R.id.ll_tablist)
        LinearLayout tabList;

    }
}
